<template>
  <el-dialog v-if="load" title="发票文件导入" v-model="visible" append-to-body :close-on-click-modal="false" :z-index="2000" width="1200px" @close="handleClose">
    <el-form ref="formRef" :model="form">
      <el-form-item>
        <VUpload v-model="form.contract_list" multiple edit :params="{ is_origin: 2 }" :types="['jpg', 'jpeg', 'png', 'pdf']" :disabled="!!form.i_id && isView"
          title="开票合同" :fileSize="50"/>
      </el-form-item>
      <el-form-item>
        <VUpload v-model="form.deliver_list" multiple edit :params="{ is_origin: 2 }" :types="['jpg', 'jpeg', 'png', 'pdf']" :disabled="!!form.i_id && isView"
          title="送货清单" :fileSize="50"/>
      </el-form-item>
      <el-form-item>
        <VUpload v-model="form.invoice_list" multiple edit :params="{ is_origin: 2 }" :types="['jpg', 'jpeg', 'png', 'pdf']" :disabled="!!form.i_id && isView"
          title="发票文件" :fileSize="50"/>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="close">关 闭</el-button>
      <el-button type="primary" :loading="loadings.submit" @click="submit">保 存</el-button>
    </template>
  </el-dialog>
</template>

<script setup>
import api from '../api'
import { ElMessage } from 'element-plus'
import { resMessage } from '@/utils/res'
const props = defineProps({
  options: { type: Object, default: () => ({}) },
})
const emit = defineEmits(['close', 'success'])

const load = ref(false)
const visible = ref(false)
let loadings = reactive({})
const form = ref({})
const formRef = ref()

const validator = (rule, value, cb) => {
  console.log('value', value)
  if (!value || !value.length) {
    cb(new Error('不能为空'))
    return
  }
  cb()
}

// 打开弹窗
const open = async () => {
  if (!load.value) {
    load.value = true
    await 1
  }
  form.value = {contract_list: [],deliver_list:[],invoice_list:[]}
  loadings = reactive({})
  visible.value = true
}
// 关闭弹窗
const close = () => {
  visible.value = false
}

const handleClose = () => {
  emit('close')
}
// 提交
const submit = async() => {
  if (!await formRef?.value.validate()) return
  loadings.submit = true
  api.importFile(form.value).then(res => {
    resMessage({
      res,
      allMsg:true,
      success: () => {
        ElMessage.success('操作成功')
        emit('success')
        close()
        query()
      }
    })
  }).finally(() => {
    loadings.submit = false
  })
}

defineExpose({ open })
</script>